<?php
error_reporting(0);
session_start();
// Create new $_SESSION variables corresponding with the fields of the associated forms.
$_SESSION['Name']= $_POST['Name'];
$_SESSION['Email']= $_POST['Email'];
$_SESSION['Phone_Num']= $_POST['Phone_Num'];
$_SESSION['Extension']= $_POST['Extension'];
$_SESSION['Marist_Status']= $_POST['Marist_Status'];
$_SESSION['Campus']= $_POST['Campus'];
$_SESSION['Comments']= $_POST['Comments'];

// Book form fields
$_SESSION['Book_Input']= $_POST['Book_Input'];
$_SESSION['Author']= $_POST['Author'];
$_SESSION['Book_Title']= $_POST['Book_Title'];
$_SESSION['Book_Publisher']= $_POST['Book_Publisher'];
$_SESSION['Book_PubYear']= $_POST['Book_PubYear'];
$_SESSION['ISBN']= $_POST['ISBN'];
$_SESSION['Edition']= $_POST['Edition'];
$_SESSION['Book_Format']= $_POST['Book_Format'];
$_SESSION['Book_Priority']= $_POST['Book_Priority'];

//DVD form fields
$_SESSION['DVD_Title']= $_POST['DVD_Title'];
$_SESSION['Director']= $_POST['Director'];
$_SESSION['Distributor']= $_POST['Distributor'];
$_SESSION['Release_Date']= $_POST['Release_Date'];
$_SESSION['DVD_Priority']= $_POST['DVD_Priority'];

//Periodical form fields
$_SESSION['Periodical_Title']= $_POST['Periodical_Title'];
$_SESSION['Periodical_Publisher']= $_POST['Periodical_Publisher'];
$_SESSION['Periodical_PubYear']= $_POST['Periodical_PubYear'];
$_SESSION['Periodical_Format']= $_POST['Periodical_Format'];
$_SESSION['Periodical_Priority']= $_POST['Periodical_Priority'];

//Website form fields
$_SESSION['Website_Title']= $_POST['Website_Title'];
$_SESSION['Subject_Guide']= $_POST['Subject_Guide'];
$_SESSION['URL']= $_POST['URL'];
$_SESSION['Website_PubYear']= $_POST['Website_PubYear'];
$_SESSION['Website_Priority']= $_POST['Website_Priority'];


if (isset($_SESSION['simpleCaptchaAnswer']) && $_POST['captchaSelection'] == $_SESSION['simpleCaptchaAnswer']) {

     // START "CAPTCHA CORRECTLY VERIFIED" ELSE BLOCK
     // CODE TO HANDLE SUCCESSFUL VERIFICATION
    
                    $_SESSION['incorrectCaptcha'] = null;
                    $_SESSION['incorrectCaptcha'] = 'false';
            
    function randomAlphaNum($length){ 
    
        $rangeMin = pow(36, $length-1); //smallest number to give length digits in base 36 
        $rangeMax = pow(36, $length)-1; //largest number to give length digits in base 36 
        $base10Rand = mt_rand($rangeMin, $rangeMax); //get the random number 
        $newRand = base_convert($base10Rand, 10, 36); //convert it 
        return $newRand; //spit it out 
    
    } 
    $referenceNo = randomAlphaNum(10);
    
    $numBooks = sizeOf($_SESSION['Book_Title'])-1;
    $numDVDs = sizeOf($_SESSION['DVD_Title'])-1;
    $numPeriodicals = sizeOf($_SESSION['Periodical_Title'])-1;
    $numWebsites = sizeOf($_SESSION['Website_Title'])-1;
                
                
$to      = 'weon.yuan1@marist.edu'; // Change the email address and other fields to be displayed on the email depending on the associated form
$subject = 'Reference No. '.$referenceNo;
$message ='
Name: '. $_SESSION['Name'] . '
Email: '. $_SESSION['Email'] . '
Telephone Number: '. $_SESSION['Phone_Num'] .'
Extension: '. $_SESSION['Extension'] .'
Marist Status: '. $_SESSION['Marist_Status'] .' 
Marist Campus: '. $_SESSION['Campus'] .' 
Comments: '. $_SESSION['Comments'];

for ($i = 1; $i <= $numBooks; $i++) {
$message .= '

BOOK '. $i .'
Title: '. $_SESSION['Book_Title'][$i] .'
Author: '. $_SESSION['Author'][$i] .'
Publisher: '. $_SESSION['Book_Publisher'][$i] .'
Publication Year:  '. $_SESSION['Book_PubYear'][$i] .'
ISBN: '. $_SESSION['ISBN'][$i] .'
Edition: '. $_SESSION['Edition'][$i] .'
Format: '. $_SESSION['Book_Format'][$i] .'
Priority: '. $_SESSION['Book_Priority'][$i];
}

for ($j = 1; $j <= $numDVDs; $j++) {
$message .= '

DVD '. $j .'
Title: '. $_SESSION['DVD_Title'][$j] .'
Director: '. $_SESSION['Director'][$j] .'
Distributor: '. $_SESSION['Distributor'][$j] .'
Release Date: '. $_SESSION['Release_Date'][$j] .'
Priority: '. $_SESSION['DVD_Priority'][$j];
}

for ($k = 1; $k <= $numPeriodicals; $k++) {
$message .= '

PERIODICAL '. $k .'
Title: '. $_SESSION['Periodical_Title'][$k] .'
Publisher: '. $_SESSION['Periodical_Publisher'][$k] .'
Publication Year: '. $_SESSION['Periodical_PubYear'][$k] .'
Format: '. $_SESSION['Periodical_Format'][$k] .'
Priority: '. $_SESSION['Periodical_Priority'][$k];
}

for ($m = 1; $m <= $numWebsites; $m++) {
$message .= '

WEBSITE '. $m .'
Title: '. $_SESSION['Website_Title'][$m] .'
Subject Placement: '. $_SESSION['Subject_Guide'][$m] .'
URL: '. $_SESSION['URL'][$m] .'
Publication Year: '. $_SESSION['Website_PubYear'][$m] .'
Priority: '. $_SESSION['Website_Priority'][$m];
}
                
    $headers = 'From:' . $_SESSION['Email']. "\r\n" . 'X-Mailer: PHP/' . phpversion();
    
    if (mail($to, $subject, $message, $headers)) {
    	$_SESSION['Message'] = 'Thank You. Your request has been sent to James A. Cannavino Library staff (845) 575-3292. Your tracking number is: '. $referenceNo;
    } else {
      		$_SESSION['Message'] = 'An Error occurred during the submission of your form. Please try again.';
      }
        echo 1;
} else {
	//CODE IF CAPTCHA VERIFICATION FAILED
	$_SESSION['incorrectCaptcha'] = null;
	$_SESSION['incorrectCaptcha'] = 'true';
	    
	echo 0;
  }
                
?>